# 使用pygame模块
# 实现植物大战僵尸中僵尸动态行走的代码
# 图片路径：images/普通/1.png到22.png共22张图片
# []列表 ()元组 {}字典集合
# a=[1,2,3,4] 索引访问 a[0]
import pygame
import os
# 类 方法 函数
# 构造函数会自动执行
class Zombie:
    def __init__(self, x, y):
        # 类里面用到的属性
        self.x = x
        self.y = y
        self.images = []
        self.current_frame = 0
        self.animation_speed = 0.2
        self.load_images()


    def load_images(self):
        # 加载22张僵尸行走图片
        for i in range(1, 23):  # 1.png到22.png
            image_path = f"images/普通/{i}.png"
            if os.path.exists(image_path):
                image = pygame.image.load(image_path)
                self.images.append(image)
            else:
                print(f"警告: 找不到图片 {image_path}")

    def update(self):
        # 更新动画帧
        self.x-=0.5
        self.current_frame += self.animation_speed
        if self.current_frame >= len(self.images):
            self.current_frame = 0

    def draw(self, screen):
        # 绘制当前帧的僵尸图片
        if self.images:
            current_image = self.images[int(self.current_frame)]
            screen.blit(current_image, (self.x, self.y))

# 初始化pygame
pygame.init()
screen = pygame.display.set_mode((1000, 600))
pygame.display.set_caption("植物大战僵尸 - 僵尸行走")
clock = pygame.time.Clock()

# 创建僵尸实例
zombie = Zombie(800, 100)
zombie1 = Zombie(800, 200)
zombie2 = Zombie(800, 300)

# 游戏主循环
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False


    # 更新僵尸动画
    zombie.update()
    zombie1.update()
    zombie2.update()

    # 绘制
    screen.fill((135, 206, 235))  # 天空蓝背景
    zombie.draw(screen)
    zombie1.draw(screen)
    zombie2.draw(screen)

    pygame.display.flip()
    clock.tick(60)

pygame.quit()
